import { getBehaviourClassByName, getGameObjectById } from "../engine";
import { Behaviour } from "../engine/Behaviour";
import { BitmapWithClipRender } from "../engine/BitmapWithClipRender";
import { checkPointInRectangle, Point, Rectangle } from "../engine/math";
import { Transform } from "../engine/Transform";
import { boolean } from "../engine/validators/boolean";
import { string } from "../engine/validators/string";
import { Display } from "./Display";


export class Drag extends Behaviour  {

    @string()
    gameObjectid;
    @boolean()
    IsTouch:boolean=false;

    mousepointdown:Point={x:0,y:0}//获取鼠标点下时的位置
    mousemove:Point={x:0,y:0}//获取鼠标移动时的位置
    initpoint:Point={x:0,y:0}//获取物体的初始位置
    isMousedown:boolean=false;//鼠标是否按下
    rectangle:Rectangle={x:0,y:0,width:0,height:0}//获取触碰物体的渲染大小及位置
    onStart():void{

        this.initpoint={x:this.gameObject.getBehaviour(Transform).x,y:this.gameObject.getBehaviour(Transform).y}
        this.gameObject.onClick= (event) =>{
            this.rectangle = getGameObjectById(this.gameObjectid).renderer.getBounds();
            this.rectangle.x=getGameObjectById(this.gameObjectid).getBehaviour(Transform).x;
            this.rectangle.y=getGameObjectById(this.gameObjectid).getBehaviour(Transform).y;
            this.isMousedown = true;
            this.mousepointdown={x:event.posX,y:event.posY}
        }
        window.addEventListener('mouseup', (e) => {
            this.isMousedown = false;
            console.log("mouseup")
        });
        window.addEventListener('mousemove', (e) => {
            this.mousemove.x=e.clientX
            this.mousemove.y=e.clientY 
            
        });
    }    
    onUpdate() {


        if(this.isMousedown){
            this.IsTouch = checkPointInRectangle(this.mousemove, this.rectangle)
           this.gameObject.getBehaviour(Transform).x=this.mousemove.x-this.mousepointdown.x+this.initpoint.x;
           this.gameObject.getBehaviour(Transform).y=this.mousemove.y-this.mousepointdown.y+this.initpoint.y;
        }else{
            this.IsTouch = false;
            this.gameObject.getBehaviour(Transform).x= this.initpoint.x
            this.gameObject.getBehaviour(Transform).y= this.initpoint.y
        }

    }

}